package com.zhuozhengsoft.pageoffice6springboot2back.controller;

import com.zhuozhengsoft.pageoffice.FileSaver;
import com.zhuozhengsoft.pageoffice.OpenModeType;
import com.zhuozhengsoft.pageoffice.PageOfficeCtrl;
import com.zhuozhengsoft.pageoffice6springboot2back.po.DefineDataRegionResult;
import com.zhuozhengsoft.pageoffice6springboot2back.util.AjaxResult;
import com.zhuozhengsoft.pageoffice6springboot2back.util.DataRegionUtil;
import com.zhuozhengsoft.pageoffice6springboot2back.util.GetDirPathUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@RestController
@RequestMapping(value = "/DataRegionEdit")
public class DataRegionEditController {

    //获取doc目录的磁盘路径
    private final String dir = GetDirPathUtil.getDirPath() + "static/doc/";

    public DataRegionEditController() throws IOException {
    }

    @RequestMapping(value = "/Word")
    public AjaxResult<DefineDataRegionResult> openFile(HttpServletRequest request) {
        PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
        poCtrl.webOpen("/doc/DataRegionEdit/test.docx", OpenModeType.docNormalEdit, "张三");

        DataRegionUtil.clearDataRegionList(); // 清空数据区域列表
        DataRegionUtil.addDefineDataRegion("PO_Guarantor", "[担保人]");
        DataRegionUtil.addDefineDataRegion("PO_SupplierAddress", "[供货单位地址]");
        DataRegionUtil.addDefineDataRegion("PO_BuyerAddress", "[购货单位地址]");
        DataRegionUtil.addDefineDataRegion("PO_No", "[合同编号]");
        DataRegionUtil.addDefineDataRegion("PO_GuarantorPhone", "[担保人手机]");
        DataRegionUtil.addDefineDataRegion("PO_ProductName", "[产品名称]");
        DataRegionUtil.addDefineDataRegion("PO_Buyer", "[购货单位]");
        DataRegionUtil.addDefineDataRegion("PO_Supplier", "[供货单位]");

        DefineDataRegionResult defineDataRegionResult = new DefineDataRegionResult();
        defineDataRegionResult.setDataRegions(DataRegionUtil.getDataRegionList());
        defineDataRegionResult.setPoHtml(poCtrl.getHtml());

        return AjaxResult.success(defineDataRegionResult);
    }

    @RequestMapping("save")
    public void save(HttpServletRequest request, HttpServletResponse response) {
        FileSaver fs = new FileSaver(request, response);
        fs.saveToFile(dir + "DataRegionEdit/" + fs.getFileName());
        fs.close();
    }

}
